# 多服务容器 Dockerfile
# 包含 MySQL 和 Redis 服务
FROM ubuntu:20.04

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive
ENV MYSQL_ROOT_PASSWORD=123456
ENV REDIS_PASSWORD=123456

# 安装必要的软件包
RUN apt-get update && apt-get install -y \
    mysql-server \
    redis-server \
    supervisor \
    curl \
    wget \
    gnupg \
    lsb-release \
    && rm -rf /var/lib/apt/lists/*

# 创建必要的目录
RUN mkdir -p /var/run/mysqld \
    && mkdir -p /var/run/redis \
    && mkdir -p /var/log/supervisor \
    && mkdir -p /etc/supervisor/conf.d

# 配置MySQL
RUN sed -i 's/bind-address.*/bind-address = 0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf \
    && sed -i 's/# max_connections.*/max_connections = 1000/' /etc/mysql/mysql.conf.d/mysqld.cnf \
    && sed -i 's/# innodb_buffer_pool_size.*/innodb_buffer_pool_size = 256M/' /etc/mysql/mysql.conf.d/mysqld.cnf

# 配置Redis
RUN sed -i 's/# requirepass.*/requirepass 123456/' /etc/redis/redis.conf \
    && sed -i 's/bind.*/bind 0.0.0.0/' /etc/redis/redis.conf \
    && sed -i 's/# maxmemory.*/maxmemory 256mb/' /etc/redis/redis.conf \
    && sed -i 's/# maxmemory-policy.*/maxmemory-policy allkeys-lru/' /etc/redis/redis.conf

# 创建supervisor配置文件
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 创建启动脚本
COPY start-services.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/start-services.sh

# 暴露端口
EXPOSE 3306 6379

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:6379/ping || mysqladmin ping -h localhost -u root -p123456 || exit 1

# 启动服务
CMD ["/usr/local/bin/start-services.sh"] 